package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserCreditPoints;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class CreditPointsDaoImpl extends TempDataObjectDao<UserCreditPoints> {

    public UserCreditPoints getCreditPoints(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_credit_points where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserCreditPoints data = parse(rs);
                return data;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    @Override
    protected boolean addData(UserCreditPoints data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_credit_points(userId,creditPoints,bannedTime,lastResetTime) values(?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setLong(i++, data.getCreditPoints());
            call.setLong(i++, data.getBannedTime());
            call.setLong(i++, data.getLastResetTime());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserCreditPoints data) {
        PreparedStatement call = null;
        String sql = "update t_u_credit_points SET creditPoints=?,bannedTime=?,lastResetTime=? where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getCreditPoints());
            call.setLong(i++, data.getBannedTime());
            call.setLong(i++, data.getLastResetTime());
            call.setLong(i++, data.getUserId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserCreditPoints parseData(ResultSet rs) throws SQLException {
        UserCreditPoints data = new UserCreditPoints();
        data.setUserId(rs.getLong("userId"));
        data.setCreditPoints(rs.getLong("creditPoints"));
        data.setBannedTime(rs.getLong("bannedTime"));
        data.setLastResetTime(rs.getLong("lastResetTime"));
        return data;
    }
}
